package com.dap.service;


import com.dap.utils.RedisUtil;
import com.dap.utils.RedisUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Service;
import redis.clients.jedis.Jedis;

@Service
@ComponentScan()
public class RedisService {
    @Value("${host}")
    private String host;
    @Value("${port}")
    private int port;



    /**
     * 存值
     *  @param key
     * @param value*/

    public String setValue(String key, String field, String value) {
        Jedis jedis=new Jedis(host, port);
        Long result = jedis.hset(key.getBytes(), field.getBytes(), value.getBytes());
        if (result>0L){
            return "set-success";
        };
        if (result==0L){
            return "set-noChange";
        }

        return "set-failed";
    }


    /**
     * 取值
     *
     * @param key
     * @return
     */
    public String getValue(String key, String field) {
        Jedis jedis = new Jedis(host, port);
        String result = jedis.hget(key,field);

        return "key="+key+"****field="+field+"****value="+result;
    }

    /**
     * 修改
     *
     * @param key
     * @param value
     */
    public String update(String key, String field,String value) {
        Jedis jedis=new Jedis(host, port);
        Long result = jedis.hset(key, field, value);
        if (result>0L){
            return "update-success";
        };
        if (result==0L){
            return "update-noChange";
        }

        return "update-failed";
    }


    /**
     * 删除
     *
     * @param key
     */
    public String delete(String key, String field) {
        Jedis jedis=new Jedis(host, port);
        Long result = jedis.hdel(key, field);

        if (result>0L){
            return "delete-success";
        };
        if (result==0L){
            return "delete-noChange";
        }

        return "delete-failed";
    }


    /**
     * 删除
     *
     */
    public String deleteTest() {
        Jedis jedis= RedisUtils.getJedis();
        jedis.get("qqq");

        return "delete-failed";
    }





}
